<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增')" />
    <th:block th:include="include :: datetimepicker-css" />
    <script type="text/javascript" src="/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="/js/jutils.min.js"></script>
    <script type="text/javascript" src="/js/simple-calendar.js"></script>
    <link rel="stylesheet" href="/css/simple-calendar.css" />
    <object  id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>
        <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0/>
    </object>
    <script type="text/javascript" src="/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="/js/LodopFuncs.js"></script>
    <script type="text/javascript" src="/js/printSmall.js"></script>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form action="/system/orderhead/orderPut" method="post" class="form-horizontal m" id="form-orderhead-add" style="z-index: 0">
            <input name="idorderHead"  type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label ">客户名称：</label>
                <div class="col-sm-8">
                    <input id="clientName" name="clientName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">联系电话：</label>
                <div class="col-sm-8">
                    <input id="phoneOne" name="phoneOne" class="form-control" type="text" onblur="checkPhone(this)" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label ">紧急电话：</label>
                <div class="col-sm-8">
                    <input id="phoneTwo" name="phoneTwo" class="form-control" type="text" onblur="checkPhone(this)">
                </div>
            </div>
            <div class="form-group container">
                <label class="col-sm-3 control-label is-required">所在城市:</label>
                <select id="shi"  class="btn btn-default dropdown-toggle" style="margin-left:12px " onblur="zhenUpdate()">
                    <option value="" >所有</option>
                </select>
                <select id="zhen" class="btn btn-default dropdown-toggle" style="margin-left:12px "  name="regionClient">
                    <option value="">所有</option>
                </select>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">详细地址：</label>
                <div class="col-sm-8">
                    <textarea id="address" name="address" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label ">订金：</label>
                <div class="col-sm-8">
                    <input id="depositOrderHead" name="depositOrderHead" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">添加商品：</label>
                <div class="col-sm-8">
                    <table class="table" id="tProduct">
                        <tr>
                            <th>商品名称</th>
                            <th>商品单价</th>
                            <th>数量</th>
                        </tr>
                        <tr id="pirceTr">
                            <td><select name="productIDOrder" class="form-control m-b" th:with="type=${@product.queryProductList()}" onchange="addGoods(this.options[this.options.selectedIndex],this)" required>
                                <option value="">-&#45;&#45;请选择-&#45;&#45;</option>
                                <option th:each="dict : ${type}" th:text="${dict.nameProduct}" th:value="${dict.idProduct}"></option>
                            </select></td>
                            <td><input id="price" name="price" class="form-control" type="text" required></td>
                            <td><input name="countOrders" class="form-control" type="text" required></td>
                            <td><input type="button" class="btn-sussess" onclick="addProduct()" value="继续添加"></input></td>
                        </tr>

                    </table>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">交货日期：</label>
                <div class="col-sm-8">
                    <div class="form-group">
                        <div  onclick="showDate()" >
                            <input id="nlDate" style="pointer-events : none;"  name="appointmentDateOrderHead" class="form-control"  type="date" value="" placeholder="yyyy-MM-dd"  required>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea id="commentOrderHead" name="commentOrderHead" class="form-control"></textarea>
                </div>
            </div>
        </form>
        <div id="container" style="position: absolute ;  visibility: hidden;z-index: 1000;" onclick="getDate()" ></div>
        <!--<button id="querd" class="datebutton btn btn-default" onclick="getDate()" style="position: absolute ; visibility: hidden">确定</button>-->
        <button id="channel" class="datebutton btn btn-success"  onclick="channel()" style="position: absolute ; visibility: hidden">取消</button>
    </div>
   <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button  type="button" class="btn btn-sm btn-primary" onclick="orderPut()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button  type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        $(function(){
            $("#nlDate").click(function (event) {
                event.preventDefault();
            })
        })
        var i = 0;
        function addProduct(){
            i++;
            let oldTr=$("#pirceTr").html();
            let tr = $("<tr></tr>");
            tr.html(oldTr)
            $($(tr.children()[1]).children()[0]).attr("id","price"+i);
            console.log(oldTr);
            $("#tProduct").append(tr);
        }

        function addGoods(val,that) {
            //赋值
            let url = "/system/product/getPrice";
            let idProduct = val.value;
            console.log(idProduct)
            let param = {"idProduct": idProduct};
            $.getJSON(url, param, function (json) {
                $($(that).parent().next().children()[0]).val(json.data)
            })
        }

        var myCalendar = new SimpleCalendar('#container');
        if (/(Android)/i.test(navigator.userAgent)) {
            myCalendar.updateSize('230px', '300px');
            $("#container").css("font-size","10px");
            $("#container").css("left","25%");
            $("#container").css("top","20%");
            $("#channel").css("left","65%");
            $("#channel").css("top","75%");
            $("#querd").css("left","50%");
            $("#querd").css("top","75%");
        } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
            myCalendar.updateSize('230px', '300px');
            $("#container").css("font-size","10px");
            $("#container").css("left","25%");
            $("#container").css("top","20%");
            $("#channel").css("left","65%");
            $("#channel").css("top","75%");
            $("#querd").css("left","50%");
            $("#querd").css("top","75%");
        } else {
            myCalendar.updateSize('300px', '350px');
            $("#container").css("font-size","15px");
            $("#container").css("left","30%");
            $("#container").css("top","10%");
            $("#channel").css("left","65%");
            $("#channel").css("top","70%");
            $("#querd").css("left","51%");
            $("#querd").css("top","90%");
        }

        function showDate(){
            $("#container").css("visibility","visible");
            $(".datebutton").css("visibility","visible");
        }
        function getDate(){
            let date = myCalendar.getSelectedDay();
            console.log(date);

            date =jutils.formatDate(date,"YYYY-MM-DD");
            $("#nlDate").val(date);
            $("#container").css("visibility","hidden");
            $(".datebutton").css("visibility","hidden");
        }
        function channel(){
            $("#container").css("visibility","hidden");
            $(".datebutton").css("visibility","hidden");
        }

        var prefix = ctx + "system/orderhead"
        $("#form-orderhead-add").validate({
            focusCleanup: true
        });

        function orderPut() {
            let form = $("#form-orderhead-add").serialize();
            console.log(form)
            var phoneOne = $("#phoneOne").val();
            var zhen = $("#zhen").val();
            var nlDate  = $("#nlDate ").val();
           if(phoneOne&&zhen&&nlDate){
               let url = "/system/orderhead/add";
               $.post(url,form,function (json) {
                   let id = json.msg;
                   p(id);
                  // $.modal.openTab("添加商品","/system/ordertable/read/"+id,false)
                   /* let rows = json.rows;
                    $(rows).each(function(i,item){
                        let html = '';
                        html += '<option value='+item.idRegion+'>'+item.nameRegion+'</option>';
                        $("#shi").append(html);
                    })*/
                   $.operate.successCallback(json);
               })
           }else{
               $.modal.msgWarning("必须填写电话城市日期");
           }


            /*if ($.validate.form()) {
               // $.operate.saveTab(prefix + "/orderPut", $('#form-orderhead-add').serialize());
                $.operate.submit(url, "post", "json", param, url);
            }*/
        }

        $("input[name='dateOrderHead']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='appointmentDateOrderHead']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        $(function(){
            $("#shi").html("");
            let url = "/system/region/listById";
            let param = {"parentId":27425};
            $.getJSON(url,param,function(json){
                let rows = json.rows;
                $(rows).each(function(i,item){
                    let html = '';
                    html += '<option value='+item.idRegion+'>'+item.nameRegion+'</option>';
                    $("#shi").append(html);
                })
            })
        })
        function zhenUpdate(){
            $("#zhen").html("");
            let url = "/system/region/listById";
            let parentId = $("#shi").val();
            let param = {"parentId":parentId};
            $.getJSON(url,param,function(json){
                let rows = json.rows;
                $(rows).each(function(i,item){
                    let html = '';
                    html += '<option value='+item.idRegion+'>'+item.nameRegion+'</option>';
                    $("#zhen").append(html);
                })
            })
        }

       /* function submitHandler() {
            if ($.validate.form()) {
                var data = $("#form-user-edit").serializeArray();
                var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
                var roleIds = $.form.selectCheckeds("role");
                var postIds = $.form.selectSelects("post");
                data.push({"name": "status", "value": status});
                data.push({"name": "roleIds", "value": roleIds});
                data.push({"name": "postIds", "value": postIds});
                $.operate.saveTab(prefix + "/orderPut", data);
            }
        }*/

        /** 关闭选项卡 */
        var closeItem = function(dataId){
            var topWindow = $(window.parent.document);
            if($.common.isNotEmpty(dataId)){
                window.parent.$.modal.closeLoading();
                // 根据dataId关闭指定选项卡
                $('.menuTab[data-id="' + dataId + '"]', topWindow).remove();
                // 移除相应tab对应的内容区
                $('.mainContent .RuoYi_iframe[data-id="' + dataId + '"]', topWindow).remove();
                return;
            }
            var panelUrl = window.frameElement.getAttribute('data-panel');
            $('.page-tabs-content .active i', topWindow).click();
            if($.common.isNotEmpty(panelUrl)){
                $('.menuTab[data-id="' + panelUrl + '"]', topWindow).addClass('active').siblings('.menuTab').removeClass('active');
                $('.mainContent .RuoYi_iframe', topWindow).each(function() {
                    if ($(this).data('id') == panelUrl) {
                        $(this).show().siblings('.RuoYi_iframe').hide();
                        return false;
                    }
                });
            }
        }
        
        function checkPhone(obj) {
            console.log(obj);
            var re = /^1([23578])\d{9}$/;
            console.log(obj.value);
            if (!re.test(obj.value)) {
                alert("请输入正确的电话号码!");
                obj.value = "";
            }
        }


    </script>
</body>
</html>